//
//  HTRecentAlertScreen.swift
//  MagaCore
//
//  Created by 罗林轩 on 2024/8/27.
//

import SwiftUI
import SDWebImageSwiftUI
// MARK: - 这个页面是用来显示历史播放的第一条

struct HTRecentAlertScreen: View {
    @State private var var_anime: HTAnime?
    @EnvironmentObject private var var_store: HTAppStore
    @State private var op: Double = 1
    @State private var var_showPushToVideoDetail : Bool = false
    var body: some View {
        if var_anime != nil {
            HStack (){
                NavigationLink(destination: HTVideoDetailScreen(var_id: var_anime!.var_id, var_source: "9"), isActive: $var_showPushToVideoDetail) {
                    EmptyView()
                }
                
                WebImage(url: URL(string: var_anime?.var_cover ?? ""))
                    .resizable()
                    .padding(.top,2)
                    .padding(.leading,2)
                    .padding(.bottom,2)
                    .cornerRadius(12, corners: .allCorners)
                    .frame(width: 76)
                    .layoutPriority(1)
                    .onTapGesture {
                        /** 点击播放*/
                        var_showPushToVideoDetail = true
                    }

                VStack(alignment: .leading) {
                    Text(var_anime?.var_title ?? "")
                        .lineLimit(1)
                        .foregroundStyle(Color.white)
                        .padding(.leading, 10)
                        .truncationMode(.tail)
                        
                    Text("FROM \(String(var_anime?.var_startTime ?? "00:00"))" )
                        .font(.system(size: 24,weight: .heavy))
                        .foregroundStyle(Color.white)
                        .padding(.leading, 10)
                }
                .layoutPriority(0)
                Spacer()
                Button {
                    /** 点击播放*/
                    var_showPushToVideoDetail = true
                } label: {
                    Text("Watch Now")
                        .frame(alignment: .center)
                        .font(.system(size: 12,weight: .heavy))
                        .foregroundStyle(Color.black)
                        .padding(.leading,12)
                        .padding(.trailing,12)
                        .padding(.top,6)
                        .padding(.bottom,6)
                }
                .layoutPriority(1)
                .background(Color.white)
                .cornerRadius(4, corners: .allCorners)
                Spacer()
            }
            .onTapGesture {
                hide(seconds: 0)
            }
            .frame(width: var_screenWidth - 20,height: 120)
            .background(Color.blue)
            .opacity(op)
            .cornerRadius(12, corners: .allCorners)
        } else {
            Text("")
                .onAppear{
                    var_store.dispatch(HTEnumAnimeAction.ht_recentPlayList(callback: { animes in
                        self.var_anime = ( animes ?? []).first
                        if var_anime != nil {
                            hide(seconds: 5)
                        }
                    }))
                }
        }
    }
    
    func hide(seconds: Double) {
        DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
            withAnimation(.easeInOut(duration: 1)){
                op = 0
            }
        }
    }
}
